package channel

import (
	"context"
	"errors"

	schema "gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/schema/tenant/channel"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/rpc"
	"github.com/zeromicro/go-zero/core/logc"
	"gorm.io/gorm"
)

type TenantChannelUsdtModel struct {
	ctx context.Context
	db  *gorm.DB
}

func NewTenantChannelUsdtModel(ctx context.Context, db *gorm.DB) *TenantChannelUsdtModel {
	return &TenantChannelUsdtModel{
		ctx: ctx,
		db:  db,
	}
}

func (model *TenantChannelUsdtModel) getDb() *gorm.DB {
	if model.db != nil {
		return model.db
	}
	return svc.Db
}

func (model *TenantChannelUsdtModel) CreateUsdt(usdt *schema.TenantChannelUsdt) error {
	res := model.getDb().Create(usdt)
	if err := res.Error; err != nil {
		logc.Error(model.ctx, "tenantChannel-CreateUsdt, err", err)
		return err
	}
	return nil
}

func (model *TenantChannelUsdtModel) GetUsdtById(Id int64) (schema.TenantChannelUsdt, error) {
	var usdt schema.TenantChannelUsdt
	res := model.getDb().First(&usdt, Id)
	if err := res.Error; errors.Is(err, gorm.ErrRecordNotFound) {
		logc.Error(model.ctx, "tenantChannel-GetUsdtById-0, err", err)
		return schema.TenantChannelUsdt{}, err
	} else if err != nil {
		logc.Error(model.ctx, "tenantChannel-GetUsdtById-1, err", err)
		return schema.TenantChannelUsdt{}, err
	}
	return usdt, nil
}

func (model *TenantChannelUsdtModel) GetUsdtByType(usdtType string) ([]schema.TenantChannelUsdt, error) {
	var usdtList []schema.TenantChannelUsdt
	res := model.getDb().Where("usdt_type =?", usdtType).Find(&usdtList)
	if err := res.Error; errors.Is(err, gorm.ErrRecordNotFound) {
		logc.Error(model.ctx, "tenantChannel-GetUsdtByType-0, err", err)
		return nil, err
	} else if err != nil {
		logc.Error(model.ctx, "tenantChannel-GetUsdtByType-1, err", err)
		return nil, err
	}
	return usdtList, nil
}

func (model *TenantChannelUsdtModel) GetAllUsdt() ([]schema.TenantChannelUsdt, error) {
	var usdtList []schema.TenantChannelUsdt
	res := model.getDb().Find(&usdtList)
	if err := res.Error; err != nil {
		logc.Error(model.ctx, "tenantChannel-GetAllUsdt, err", err)
		return nil, err
	}
	return usdtList, nil
}

func (model *TenantChannelUsdtModel) FindUsdtList(page *rpc.Page) (count int64, res []schema.TenantChannelUsdt, err error) {
	if page == nil {
		return
	}

	dbModel := model.getDb().Model(&schema.TenantChannelUsdt{})
	dbRes := dbModel.Count(&count)
	if err = dbRes.Error; err != nil {
		return
	}

	dbRes = dbModel.Limit(int(page.PageSize)).
		Offset(int((page.Page - 1) * page.PageSize)).
		Find(&res)
	if err = dbRes.Error; err != nil {
		logc.Error(model.ctx, "tenantChannel-GetAllUsdt, err", err)
		return
	}

	return
}

func (model *TenantChannelUsdtModel) UpdateUsdt(usdt *schema.TenantChannelUsdt) error {
	res := model.getDb().Save(usdt)
	if err := res.Error; err != nil {
		logc.Error(model.ctx, "tenantChannel-UpdateUsdt, err", err)
		return err
	}
	return nil
}

func (model *TenantChannelUsdtModel) DeleteUsdt(id int64) error {
	res := model.getDb().Delete(&schema.TenantChannelUsdt{}, id)
	if err := res.Error; err != nil {
		logc.Error(model.ctx, "tenantChannel-DeleteUsdt, err", err)
		return err
	}
	return nil
}
